#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H
#include"tool.h"
#include"signal.h"
#include"cmserver.h"
#include<atomic>
#include<mutex>
#include<map>

#define NET_INSTANCE NetWorkManager::getInstance
#define NET_SOCKET NetWorkManager::getInstance()->getSocketManager

using NetMsg = Json::Value;

class NetWorkManager
{
    GET_SINGLE_OBJECT(NetWorkManager)
public:
    NetWorkManager(const NetWorkManager&) = delete;
    NetWorkManager& operator=(const NetWorkManager&) = delete;

    void startServer();

    Slot* add(const int& messageId, const EventHandler& evHandler);

    Slot* add(const int& messageId, const std::function<void(Json::Value&)>&);

    void dispatch(const int& messageId,Json::Value& message);

    void send(const int& fd,const int & messageId, const std::map<std::string, std::string>& msg);

    void send(const int& fd,const int & messageId, Json::Value& msg);

    PROPERTY__REF(int,m_fd,Fd)

    PROPERTY__REF(CMServer,m_socketManager,SocketManager)
private:
    NetWorkManager();
    ~NetWorkManager() {}
    std::map<int, Signal> m_signals;
    int m_curfd;

    SINGLE_ATTRIBUTES(NetWorkManager);
};


#endif // NETWORKMANAGER_H
